package cn.hl.hm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hl.hm.dao.PzfVersionMapper;
import cn.hl.hm.entity.PzfVersion;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.JacksonJsonUtil;
import cn.hl.hm.util.Result;

@Controller
@RequestMapping("/pzfVersionController")
public class PzfVersionController {
	private static Logger logger = Logger.getLogger(PzfVersionController.class);
 
	@Autowired
	private PzfVersionMapper pzfVersionMapper;

	// app版本检查更新
	@RequestMapping(value = "/version", method = RequestMethod.POST)
	public @ResponseBody String version(
			@RequestParam("channelId") String channelId, // 渠道字如:CHANNEL_SELF
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("app版本检查更新：start" + JacksonJsonUtil.beanToJson(channelId) + "程序内部识别的版本号" );
		if (null == channelId || "null".equals(channelId)) {
			Result.ok(null, InfoCode.FAILURE, response);
			 
			return null;
		}
		try {
			PzfVersion record = new PzfVersion();
			//返回最新的对应渠道配置
			record.setChannelId(channelId);
			List<PzfVersion> tve = pzfVersionMapper.selectByConditions(record);
			logger.info("查看版本列表返回：" + JacksonJsonUtil.beanToJson(tve));
			if(tve!=null && tve.size()>0){
				Result.ok(tve.get(0), InfoCode.SUCCESS, response);
			}else{
				Result.ok(null, InfoCode.SUCCESS, response);
			}
			
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
	/**
	* @Title: versionList
	* @Description: 版本列表
	* @author : 大和
	* @date :2016年9月20日
	* @param record
	* @param request
	* @param response
	* @return String
	* @throws
	 */
	@RequestMapping(value = "/versionList", method = RequestMethod.POST)
	public @ResponseBody String versionList(PzfVersion record, HttpServletRequest request, HttpServletResponse response) {
		try {
			logger.info("查看版本列表：param" +record.toString());
			List<PzfVersion> tve = pzfVersionMapper.selectByConditions(record);
			logger.info("查看版本列表返回：" + JacksonJsonUtil.beanToJson(tve));
			Result.ok(tve, InfoCode.SUCCESS, response);
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
	/**
	* @Title: addNewPzfVersion
	* @Description: 版本列表
	* @author : 大和
	* @date :2016年9月20日
	* @param record
	* @param request
	* @param response
	* @return String
	* @throws
	 */
	@RequestMapping(value = "/addNewPzfVersion", method = RequestMethod.POST)
	public @ResponseBody String addNewPzfVersion(PzfVersion record, HttpServletRequest request, HttpServletResponse response) {
		try {
			logger.info("添加版本信息：param" +record.toString());
			record.setUpdateTime(new Date());
			int  i = pzfVersionMapper.insertSelective(record);
			logger.info("添加版本信息：返回" + JacksonJsonUtil.beanToJson( i ));
			Result.ok(i, InfoCode.SUCCESS, response);
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
	
	/**
	* @Title: updatePzfVersionInfo
	* @Description: 版本列表
	* @author : 大和
	* @date :2016年9月20日
	* @param record
	* @param request
	* @param response
	* @return String
	* @throws
	 */
	@RequestMapping(value = "/updatePzfVersionInfo", method = RequestMethod.POST)
	public @ResponseBody String updatePzfVersionInfo(PzfVersion record, HttpServletRequest request, HttpServletResponse response) {
		try {
			logger.info("更新版本信息：param" +record.toString());
			record.setUpdateTime(new Date());
			int  i = pzfVersionMapper.updateByPrimaryKeySelective(record);
			logger.info("更新版本信息：返回" + JacksonJsonUtil.beanToJson( i ));
			Result.ok(i, InfoCode.SUCCESS, response);
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
	
	
	@RequestMapping(value = "/deletePzfVersionInfo", method = RequestMethod.POST)
	public @ResponseBody String deletePzfVersionInfo(Integer id, HttpServletRequest request, HttpServletResponse response) {
		try {
			logger.info("删除版本信息：param" + id);
			int  i = pzfVersionMapper.deleteByPrimaryKey(id);
			logger.info("删除版本信息：返回" + JacksonJsonUtil.beanToJson( i ));
			Result.ok(i, InfoCode.SUCCESS, response);
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
	/**
	* @Title: downloadBySource
	* @Description: 发货端
	* @author : 大和
	* @date :2016年11月15日
	* @param request
	* @param response
	* @return String
	* @throws
	 */
	@RequestMapping(value = "/downloadConsigne", method = RequestMethod.GET)
	public @ResponseBody String downloadConsigne(HttpServletRequest request, HttpServletResponse response) {
		try {
			String userAgent = request.getHeader("user-agent");
			logger.info("...下载最新发货端包 ：访问来源：【"+userAgent+"】");
			String url = "";
			if(userAgent.contains("iPhone")||userAgent.contains("iPad")){
				//IOS
				url = "https://itunes.apple.com/cn/app/ding-dang-dao-fa-huo/id1141755596?mt=8";
			}else if (userAgent.contains("Android")) {
				url = "http://api.hlvan.cn/appdownload/ddd_consigne.apk";
			}else {
				url = "http://api.hlvan.cn/appdownload/ddd_consigne.apk";
			}
			logger.info("...下载最新发货端包 ：url "+"【 "+url+"】");

			response.sendRedirect(url);
			//Andriod
			Result.ok(userAgent, InfoCode.SUCCESS, response);
			 
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
	/**
	* @Title: downloadBySource
	* @Description: 司机端
	* @author : 大和
	* @date :2016年11月15日
	* @param request
	* @param response
	* @return String
	* @throws
	 */
	@RequestMapping(value = "/downloadDriver", method = RequestMethod.GET)
	public @ResponseBody String downloadDriver(HttpServletRequest request, HttpServletResponse response) {
		try {
			String userAgent = request.getHeader("user-agent");
			logger.info("...下载最新包 ：访问来源：【"+userAgent+"】");
			String url = "";
			if(userAgent.contains("iPhone")||userAgent.contains("iPad")){
				//IOS
				url = "https://itunes.apple.com/cn/app/ding-dang-dao-si-ji/id1141757989?mt=8";
			}else if (userAgent.contains("Android")) {
				url = "http://api.hlvan.cn/appdownload/ddd_driver.apk";
			}else {
				url = "http://api.hlvan.cn/appdownload/ddd_driver.apk";
			}
			logger.info("...下载最新包 ：url "+"【 "+url+"】");
			response.sendRedirect(url);
			//Andriod
			Result.ok(userAgent, InfoCode.SUCCESS, response);
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
}
